package org.chen.scene.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.scene.dto.AttractionQueryDTO;
import org.chen.common.scene.dto.FilterOptionDTO;
import org.chen.common.scene.dto.NearbyAttractionQueryDTO;
import org.chen.common.scene.dto.PagedAttractionResponse;
import org.chen.common.scene.entity.District;
import org.chen.common.result.ResultVO;
import org.chen.scene.service.AttractionService;
import org.chen.scene.service.DistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@Validated
@Tag(name = "不重要的接口", description = "不重要的接口")
public class AttractionController {
    
    @Autowired
    private AttractionService attractionService;
    
    @Autowired
    private DistrictService districtService;

    /**
     * 查询景点列表
     */
    @PostMapping("/list")
    @Tag(name = "景点查询的接口")
    public ResultVO<PagedAttractionResponse> getAttractions(@Valid @RequestBody AttractionQueryDTO queryDTO) {
        try {
            PagedAttractionResponse response = attractionService.getAttractions(queryDTO);

            return ResultVO.success(response);

        } catch (Exception e) {
            log.error("查询景点列表异常", e);
            return ResultVO.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 先从数据库，再从Ctrip查询景点列表
     */
    @PostMapping("/list_data")
    @Tag(name = "景点查询的接口")
    public ResultVO<PagedAttractionResponse> getAttractionsByDatas(@Valid @RequestBody AttractionQueryDTO queryDTO) {
        try {
            PagedAttractionResponse response = attractionService.getAttractionsFromDatabase(queryDTO);

            return ResultVO.success(response);

        } catch (Exception e) {
            log.error("查询景点列表异常", e);
            return ResultVO.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 删除景点
     */
    @DeleteMapping("/delete/{poId}")
    public ResultVO<String> deleteAttractions(@PathVariable String poId) {
        try {
            boolean b = attractionService.deleteAttractionById(poId);

            if (b) {
                return ResultVO.success("删除成功");
            } else {
                return ResultVO.fail("删除失败，景点可能不存在");
            }

        } catch (Exception e) {
            log.error("查询景点列表异常", e);
            return ResultVO.fail("查询失败：" + e.getMessage());
        }
    }


    /**
     * 根据城市名称或经纬度查询附近景点
     */
    @PostMapping("/nearby")
    @Tag(name = "附近景点查询接口")
    public ResultVO<PagedAttractionResponse> getNearbyAttractions(@Valid @RequestBody NearbyAttractionQueryDTO queryDTO) {
        try {
            PagedAttractionResponse response = attractionService.getNearbyAttractions(queryDTO);

            return ResultVO.success(response);

        } catch (Exception e) {
            log.error("查询附近景点异常", e);
            return ResultVO.fail("查询失败：" + e.getMessage());
        }
    }

    /**
     * 获取筛选条件
     */
    @GetMapping("/filters")
    public ResultVO<List<FilterOptionDTO>> getFilterOptions() {
        try {
            List<FilterOptionDTO> filters = attractionService.getFilterOptions();
            return ResultVO.success(filters);
        } catch (Exception e) {
            log.error("获取筛选条件异常", e);
            return ResultVO.fail("获取筛选条件失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有区域
     */
    @GetMapping("/districts")
    public ResultVO<List<District>> getDistricts() {
        try {
            List<District> districts = districtService.list();
            return ResultVO.success(districts);
        } catch (Exception e) {
            log.error("获取区域列表异常", e);
            return ResultVO.fail("获取区域列表失败：" + e.getMessage());
        }
    }
}